<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <style>
        /* 设置全局样式 */
        body, html {
            margin: 0;
            padding: 0;
            height: 100%;
            font-family: Arial, sans-serif;
        }

        /* 设置背景图片 */
        body {
            background-image: url('login.jpg'); /* 替换为你的背景图片路径 */
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
        }

        /* 登录容器样式 */
        .login-container {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(255, 255, 255, 0.8); /* 半透明背景 */
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        /* 表单样式 */
        form {
            display: flex;
            flex-direction: column;
        }

        label {
            margin-top: 10px;
            font-weight: bold;
        }

        input {
            padding: 10px;
            margin-top: 5px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            margin-top: 20px;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }

        /* 响应式设计 */
        @media (max-width: 480px) {
            .login-container {
                width: 90%;
            }
        }

        /* 没有账号链接样式 */
        .no-account-link {
            margin-top: 20px;
            color: #007bff;
            cursor: pointer;
            text-decoration: underline;
        }

        .no-account-link:hover {
            color: #0056b3;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="login-container">
    <h2>Login</h2>
    <form id="loginForm">
        <label for="username">用户名:</label>
        <input type="text" id="username" name="username" required>
        <label for="password">密码:</label>
        <input type="password" id="password" name="password" required>
        <button type="submit">Login</button>
    </form>
    <div id="response-message" style="margin-top: 10px; color: red;"></div>
    <div class="no-account-link" id="registerLink">没有账号？去注册</div>
</div>
<script>
    $(document).ready(function() {
        $('#loginForm').submit(function(event) {
            event.preventDefault();

            const username = $('#username').val();
            const password = $('#password').val();

            $.ajax({
                url: 'http://127.0.0.1:9999/login',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({
                    "username": username,
                    "password": password
                }),
                success: function(response) {
                    console.log(response);
                    // 保存token到localStorage
                    if (response.code == 0) {
                        localStorage.setItem('token', response.data);
                        $('#response-message').text('Login successful! Token saved.');
                        window.location.href = "admin.html";
                    } else {
                        alert("login failed:" + response.message);
                        $('#response-message').text('Login failed: ' + response.message);
                    }
                },
                error: function(xhr, status, error) {
                    $('#response-message').text('Login failed: ' + xhr.responseText);
                }
            });
        });

        // 添加注册链接的点击事件
        $('#registerLink').click(function() {
            window.location.href = "register.html";
        });
    });
</script>
</body>
</html>